/*
 * @Author: luojianfeng
 * @Date: 2022-12-13 10:26:49
 * @LastEditTime: 2023-11-21 10:51:16
 * @LastEditors: luojianfeng
 * @Description:
 */
import type {
  AxiosError,
  AxiosRequestConfig,
  RawAxiosRequestHeaders
} from 'axios'
import axios from 'axios'
import { showNotify } from 'vant'
import { TypeResponse } from '~/types/request/http'

const baseUrl = import.meta.env.VITE_API_BASE_URL

const service = axios.create({
  baseURL: baseUrl,
  timeout: 30 * 1000,
  // 请求是否携带cookie
  // withCredentials: true,
  headers: {
    'Cache-Control': 'no-cache'
  }
})

// 请求拦截器
service.interceptors.request.use(
  (config: any) => {
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const { status } = response
    if (status < 200 || status >= 300) {
      // 统一处理http错误，或者处理后抛到业务代码 TODO
    }
    if (status == 200 && response?.data?.code === 1) {
      showNotify({ type: 'danger', message: response.data.msg })

      return Promise.reject(response)
    }
    return response
  },
  (err) => {
    console.log(err)
    const { status, data } = err.response

    // 根据返回的http状态码做不同的处理，比如错误提示等 TODO
    switch (status) {
      case 200:
        console.log(data)
        if (data.code == 1 && data.msg) {
          showNotify({ type: 'danger', message: data.msg })
        }
        break
      case 400:
        showNotify({ type: 'danger', message: data.msg })

        break
      case 401:
        // 鉴权失败
        break
      case 403:
        // 没有权限
        break
      case 500:
        // 服务端错误
        showNotify({ type: 'danger', message: '服务异常' })
        break

      default:
        break
    }

    return Promise.reject(err)
  }
)

// 封装一层以更好的统一定义接口返回的类型
const request = <T>(config: AxiosRequestConfig): Promise<TypeResponse<T>> => {
  return new Promise((resolve, reject) => {
    service
      .request<TypeResponse<T>>(config)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err: Error | AxiosError) => {
        reject(err)
      })
  })
}
/**
 * get请求
 * @param url
 * @param params
 * @param headers
 * @returns
 */
export const get = <T>(
  url: string,
  params?: Record<string, string | number>,
  headers?: RawAxiosRequestHeaders
) => request<T>({ url, method: 'GET', params, headers })
/**
 * post 请求
 * @param url
 * @param data
 * @param headers
 * @returns
 */
export const post = <T>(
  url: string,
  data?: Record<string, any>,
  headers?: RawAxiosRequestHeaders
) => request<T>({ url, method: 'POST', data, headers })
/**
 * delete 请求
 * @param url
 * @param data
 * @param headers
 * @returns
 */
export const deleteApi = <T>(
  url: string,
  data?: Record<string, any>,
  headers?: RawAxiosRequestHeaders
) => request<T>({ url, method: 'delete', data, headers })
/**
 * put 请求
 * @param url
 * @param data
 * @param headers
 * @returns
 */
export const put = <T>(
  url: string,
  data?: Record<string, any>,
  headers?: RawAxiosRequestHeaders
) => request<T>({ url, method: 'put', data, headers })

export default request
